/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.openbookformat.obffinder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;
import java.util.TreeMap;
import org.openide.loaders.DataObject;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;

/**
 *
 * @author MUFEED
 */
public class FindChildren extends Children.Keys implements Observer {
    java.util.Map findChapters;

    public FindChildren() {
        this.findChapters = new HashMap<DataObject, ArrayList<FindResult>>();
    }

    @Override
    protected void addNotify() {
        setKeys(findChapters.keySet());
    }


    @Override
    protected Node[] createNodes(Object key) {
        //System.out.println("Key[FC]");
        FindUtilNode node = new FindUtilNode(
                (ArrayList) findChapters.get((DataObject) key));
        AbstractNode absNode = new AbstractNode(node);
        absNode.setDisplayName(((DataObject) key).getName() + " [" +
                ((ArrayList) findChapters.get((DataObject) key)).size() + "]");
        return new Node[] { absNode };
    }

    @Override
    public void update(Observable o, Object obj) {
        System.out.println("Update Received!!!!");
        findChapters = (HashMap) obj;
        setKeys(findChapters.keySet());
        refresh();

        Iterator it = findChapters.keySet().iterator();
        while(it.hasNext())
            refreshKey(it.next());

    }
}
